package sk.tonyb.crawler.configuration;

import org.apache.log4j.Logger;

import sk.tonyb.crawler.codebook.ConfigurationFile;

/** 
 * Class, which store configuration values. <br> 
 * 
 * @author Anton Balucha
 * @since 16.10.2011
 * @last_modified 16.10.2011
 */
public class ConfigValues {
	
	/** Log4j logger. */
	private static Logger logger = Logger.getLogger(ConfigValues.class);
	
	private static ConfigValues configValues = null;
	
	private String pageStart = ConfigurationFile.PAGE_START_DEFAULT_VALUE;
	private int pageCountMax = ConfigurationFile.PAGE_COUNT_MAX_DEFAULT_VALUE;
	private String downloadLocation = ConfigurationFile.DOWNLOADED_LOCATION_DEFAULT_VALUE;
	
	/** Constructor. */
	private ConfigValues() {
		this.init();
	}
	
	/** Method, which return unique instance of {@link ConfigValues}. */
	public static synchronized ConfigValues getInstance() {
		
		if(ConfigValues.configValues == null) {
			ConfigValues.configValues = new ConfigValues();
		}
		
		return ConfigValues.configValues;
	}
	
	/** Method, which initialize values. */
	private synchronized void init() {
		this.readPageStart();
		this.readPageCountMax();
		this.readDownloadLocation();
	}

	/** Method, which read, check and repair starting page. */
	private synchronized void readPageStart() {
		
		String pageStart = ConfigReader.getInstance().getPageStart();
		this.pageStart = (pageStart == null || pageStart.trim().isEmpty() ? this.pageStart : pageStart.trim());
	}

	/** Method, which read, check and repair number of maximum downloaded pages. */
	private synchronized void readPageCountMax() {
		
		String pageCountMaxRaw = ConfigReader.getInstance().getPageCountMax();
		int pageCountMax = ConfigurationFile.PAGE_COUNT_MAX_DEFAULT_VALUE;
		
		try {
			
			if(pageCountMaxRaw != null && pageCountMaxRaw.trim().length() > 0) {
				pageCountMax = Integer.parseInt(pageCountMaxRaw.trim());
			}

			else {
				pageCountMax = ConfigurationFile.PAGE_COUNT_MAX_DEFAULT_VALUE;
			}
		}
		
		catch(NumberFormatException e) {
			logger.error("NumberFormatException", e);
			pageCountMax = ConfigurationFile.PAGE_COUNT_MAX_DEFAULT_VALUE;
		}
		
		catch(Exception e) {
			logger.error("Exception", e);
			pageCountMax = ConfigurationFile.PAGE_COUNT_MAX_DEFAULT_VALUE;
		}
		
		this.pageCountMax = pageCountMax;
	}
	
	/** Method, which read, check and repair download location. */
	private synchronized void readDownloadLocation() {
		
		String downloadLocation = ConfigReader.getInstance().getDownloadedLocation();
		this.downloadLocation = (downloadLocation == null || downloadLocation.trim().isEmpty() ? this.downloadLocation : downloadLocation.trim());
	}
	
	/** Method, which return starting page. */
	public synchronized String getPageStart() {
		return this.pageStart;
	}
	
	/** Method, which return maximal number of downloaded pages. */
	public synchronized int getPageCountMax() {
		return this.pageCountMax;
	}
	
	/** Method, which return download location. */
	public synchronized String getDownloadLocation() {
		return this.downloadLocation;
	}
	
	/** Method, which return actual values as string. */
	public synchronized String toString() {
		return "[this.pageStart = " + this.pageStart + "] [this.pageCountMax = " + this.pageCountMax + "] [this.downloadLocation = " + this.downloadLocation + "]";
	}
}
